<?php

namespace App\Http\Controllers\Home;

use App\Models\Activitys;
use App\Models\Ads;
use App\Models\Categorys;
use App\Models\Demands;
use App\Models\Goods;
use App\Models\Messages;
use App\Models\Stores;
use App\Models\Users;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
    public function index(Request $request)
    {
//        $user_id = auth()->guard('web')->id();
//        dd($user_id);

 //       return view('home.index.index');

    }




    /**
     * banner
     * @param Request $request
     * @param 广告位id $adsense_id
     * @return bool
     */
    public function banner(Request $request){
        try{
            $params = [
                'adsense_id' => 1,
                'status' => 1,
//                'start_at' => date('Y-m-d H:i:s'),
//                'end_at' => date('Y-m-d H:i:s'),
            ];
            $lists = Ads::getList($params, ['id','adsense_id','thumb','link']);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 分类数据
     * @param Request $request
     * @return bool
     */
    public function categorys(Request $request){
        try{
            $lists = Categorys::apiCascaderList();//获取分类
            $fields = ['id','store_name','logo','call','view','praise','service_scope','hot','quality'];
            foreach ($lists as $k=>$v){
                //获取分类下的商铺
                $lists[$k]['store'] = Stores::getList(array('recommend'=>2,'cid'=>$v['id']), $fields, 5);
            }
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 热门活动
     * @param Request $request
     * @return bool
     */
    public function hotact(Request $request){
        try{
            $params = [
                'hot' => 2,
                'now_at' => date('Y-m-d H:i:s'),
            ];
            $lists = Activitys::allLists($params,['id','title','cate_pid','cate_id'], null,5);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 热销商品
     * @param Request $request
     * @return bool
     */
    public function hotgoods(Request $request){
        try{
            $lists = Goods::allLists(['hot'=>2,'status'=>1], ['id','cate_id','cate_pid','title','thumb'], null, 6);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 爬楼数据
     * @param Request $request
     * @param 广告位id $adsense_id
     * @return bool
     */
    public function climbs(Request $request){
        try{
            $lists = Categorys::apiCascaderList();//获取分类
            foreach ($lists as $k=>$v){
                //获取分类下的商铺
                $fields = ['id','store_name','logo','call','view','praise','service_scope','hot','quality'];
                $lists[$k]['store'] = Stores::allLists(['recommend'=>2,'cid'=>$v['id']],$fields, null, 4);
                foreach ($lists[$k]['store'] as $kk=>$vv){
                    $lists[$k]['store'][$kk]['view'] = strlen($vv['view'])>=4 ? '999+' : $vv['view'];
                    $lists[$k]['store'][$kk]['call'] = strlen($vv['call'])>=4 ? '999+' : $vv['call'];
                    $lists[$k]['store'][$kk]['praise'] = strlen($vv['praise'])>=4 ? '999+' : $vv['praise'];
                }
                //获取分类下的广告
                $params = [
                    'cate_id' => $v['id'],
                    'adsense_id' => 2,
                    'status' => 1,
//                    'start_at' => date('Y-m-d H:i:s'),
//                    'end_at' => date('Y-m-d H:i:s'),
                ];
                $lists[$k]['adv'] = Ads::getList($params, $fields = ['id','adsense_id','thumb','link']);
                //获取首页中间广告
                $maps = [
                    'adsense_id' => 3,
                    'status' => 1,
//                    'start_at' => date('Y-m-d H:i:s'),
//                    'end_at' => date('Y-m-d H:i:s'),
                ];
                $adv_center = Ads::getList($maps, $fields = ['id','adsense_id','thumb','link']);
                if (!empty($adv_center)){
                    if ($k == 0){
                        $lists[$k]['adv_center'] = $adv_center[0];
                    }elseif ($k == 3){
                        $lists[$k]['adv_center'] = $adv_center[1];
                    }elseif ($k == 6){
                        $lists[$k]['adv_center'] = $adv_center[2];
                    }
                }
            }
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 分类下店铺列表
     * @param Request $request
     * @param 分类id cid
     * @return bool
     */
    public function stores(Request $request){
        try{
            $cid = (int)$request->get('cid');
            if (empty($cid)) return $this->writeJson(1, null, '分类id不能为空');
            $fields = ['id','store_name','logo','call','view','praise','service','service_scope','hot','quality'];
            $params = array();
            $params['status'] = 1;
            $params['cid'] = $cid;
            $lists = Stores::allLists($params,$fields, null, 4);
            foreach ($lists as $k=>$v){
                $lists[$k]['view'] = strlen($v['view'])>=4 ? '999+' : $v['view'];
                $lists[$k]['call'] = strlen($v['call'])>=4 ? '999+' : $v['call'];
                $lists[$k]['praise'] = strlen($v['praise'])>=4 ? '999+' : $v['praise'];
            }
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 成交记录
     * @param Request $request
     * @return bool
     */
    public function transaction(Request $request){
        try{
            $lists = Messages::where('type',5)->orderBy('created_at', 'desc')->limit(4)->get('title');
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 推荐店铺轮播
     * @param Request $request
     * @return bool
     */
    public function hotstore(Request $request){
        try{
            $lists = Stores::where(array('recommend'=>2,'status'=>1))->where('status','<>',-1)->get(['logo','website']);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 首页底部数量统计
     * @param Request $request
     * @param strlen 获取字符串长度
     * @param substr_replace 一个字符串插入另一个字符串
     * @return bool
     */
    public function numbers(Request $request){
        try{
            $lists['store'] = Stores::count();
            $lists['user'] = Users::count();
            $lists['act'] = Activitys::count();
            $lists['demand'] = Demands::count();
            foreach ($lists as $k=>$v){
                if(strlen($v)>=4){
                    $lists[$k] = substr_replace($v, ',', -3, 0);
                }else{
                    $lists[$k] = $v;
                }
            }
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

}